package tools;

import com.alibaba.fastjson.JSON;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InfoPack {
    public static String RstoJson(ResultSet rsSet) {
        List users = new ArrayList<>();
        try {
            ResultSetMetaData md = rsSet.getMetaData();//获取键名
            int columnCount =  md.getColumnCount();//获取行的数量
            while (rsSet.next()) {
                Map rowData = new HashMap();//声明Map
                for (int i = 1; i <= columnCount; i++) {
                    rowData.put(md.getColumnName(i), rsSet.getObject(i));//获取键名及值
                }
                users.add(rowData);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.toJSONString(users);
    }
}
